using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using sdm.exam.domain;
using sdm.exam.domain.Abstract;
using sdm.exam.domain.DataContext;
using sdm.exam.domain.Entities;

namespace sdm.exam.test.Domain
{
    [TestClass]
    public class SemesterClassDomainTest
    {
        private SemesterClassDomain _domain = null;

        /// <summary>
        /// Initialize the test.
        /// </summary>
        [TestInitialize]
        public void Start()
        {
            Ninjector.Kernel.Bind<ISdmExamRepository>().ToConstant(new DataContext(ConnectionStrings.TestConnection));
            _domain = new SemesterClassDomain();
        }

        /// <summary>
        /// Cleanup the test.
        /// </summary>
        [TestCleanup]
        public void End()
        {
            Ninjector.Kernel.Unbind<ISdmExamRepository>();
        }

        /// <summary>
        /// Test whether or not a semesterClass is conneted up to a studyplan.
        /// </summary>
        [TestMethod]
        public void AddStudyplanToSemesterClassTest()
        {
            var studyplanDomain = new StudyPlanDomain();
            var educationDomain = new EducationDomain();
            var edu  = new Education{Name = "CS2010"};
            var semesterClass = new SemesterClass()
            { 
                Education = edu,
                EndDate = new DateTime(2013,2,28),
                StartDate = new DateTime(2010,9,2),
                Name = "Computer science"
            };

            var studyplan = new Studyplan
                                {
                                    Courses = new List<Course>
                                                  {
                                                      new Course() {Name = "SDM"}
                                                  },
                                    SemesterClass = semesterClass,
                                    IsAssignedToSemesterClass = false
                                };
            educationDomain.AddEducation(edu);
            studyplanDomain.AddStudyPlan(studyplan);
            //_domain.AddNewSemesterClass(semesterClass);

            _domain.AddStudyplanToSemesterClass(semesterClass, studyplan);

            Assert.IsNotNull(studyplan.SemesterClass);
            Assert.AreEqual(true,studyplan.IsAssignedToSemesterClass);
        }

        /// <summary>
        /// Test whether or not we get the list of semester classes.
        /// </summary>
        [TestMethod]
        public void GetListOfSemesterClassesTest()
        {
            //Ninjector.Kernel.Bind<ISdmExamRepository>().ToConstant(new DataContext(ConnectionStrings.TestConnection));

            //var domain = new SemesterClassDomain();
            List<SemesterClass> semesterClassList = null;
            semesterClassList = _domain.GetListOfSemesterClasses();

            Assert.IsNotNull(semesterClassList);
        }
        
        /// <summary>
        /// Checks whether or not the semester class i added.
        /// </summary>
        [TestMethod]
        public void AddSemesterClassTest()
        {
            //Ninjector.Kernel.Bind<ISdmExamRepository>().ToConstant(new DataContext(ConnectionStrings.TestConnection));

            //var semesterClassDomain = new SemesterClassDomain();
            const string name = "CS2010";
            var education = new Education() {Name = "Computer Science"};
            var startDate = new DateTime(2010, 9, 2);
            var endDate = new DateTime(2012, 2, 28);

            int listCountFirst = _domain.GetListOfSemesterClasses().Count;

            _domain.AddNewSemesterClass(name, startDate, endDate, education);

            int listCountSecond = _domain.GetListOfSemesterClasses().Count;

            Assert.AreNotEqual(listCountFirst, listCountSecond);
        }

        /// <summary>
        /// Edit a specific semester.
        /// </summary>
        [TestMethod]
        public void EditSemesterClassTest()
        {
            //Ninjector.Kernel.Bind<ISdmExamRepository>().ToConstant(new DataContext(ConnectionStrings.TestConnection));
            //var semesterClassDomain = new domain.DataContext.SemesterClassDomain();

            const int id = 2;

            const string newName = "CS2009";
            string oldName = "";
            var newStartDate = new DateTime(2009,9,2);
            var newEndDate = new DateTime(2011, 2, 28);
            var newEducation = new Education() { Name = "Datamatiker" };

            var list = _domain.GetListOfSemesterClasses();

            SemesterClass semesterClass = list.FirstOrDefault(item => item.Id == id);

            if (semesterClass != null) oldName = semesterClass.Name;

            _domain.EddSemesterClass(id, newName, newStartDate, newEndDate, newEducation);

            if (semesterClass != null) Assert.AreNotEqual(oldName,semesterClass.Name);
        }

        /// <summary>
        /// Deletes a semester class.
        /// </summary>
        [TestMethod]
        public void DeleteSemesterClassTest()
        {
            //Ninjector.Kernel.Bind<ISdmExamRepository>().ToConstant(new DataContext(ConnectionStrings.TestConnection));
            //var semesterClassDomain = new domain.DataContext.SemesterClassDomain();

            const int id = 3;

            int listFirst = _domain.GetListOfSemesterClasses().Count;

            _domain.DeleteSemesterClass(id);

            int listSecond = _domain.GetListOfSemesterClasses().Count;

            Assert.AreNotEqual(listFirst,listSecond);
        }
    }
}
